package com.dyylearn.servlet.request;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CheckServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("CheckServlet 被调用");

        //根据用户名来确定该用户是什么身份
        String username = req.getParameter("username");
        if("tom".equals(username)) {
            //分配
            req.setAttribute("role", "管理员");
        } else {
            req.setAttribute("role", "普通用户");
        }

        //获取分发器
        //1./manageServlet写的是要转发的servlet的url
        //2.会被解析成http://localhost:8080/servlet
        //3.forward（request，response）表示把当前servlet的request对象和response对象，传递给下一个servlet
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/manageServlet");
        requestDispatcher.forward(req, resp);
    }
}
